package learn.Thread;

public class ThreadPrintABCDeadLock {
    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();

        Thread t1 = new ThreadPrint3("a",a,b);
        Thread t2 = new ThreadPrint3("b",b,c);
        Thread t3 = new ThreadPrint3("c",c,a);
        t1.start();
        Thread.sleep(10);
        t2.start();
        Thread.sleep(10);
        t3.start();
        Thread.sleep(10);

    }
}

class ThreadPrint3 extends Thread{
    private Object aLock;
    private Object bLock;
    private String value;

    public ThreadPrint3(String value,Object a,Object b){
        this.value = value;
        aLock = a;
        bLock = b;
    }

    @Override
    public void run() {

        synchronized (aLock){
            synchronized (bLock){
                System.out.println(value);
                bLock.notifyAll();
                try {
                    aLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}